using System;
using System.Collections.Generic;
using System.Configuration;
using System.Configuration.Provider;
using System.Web.Configuration;
using System.Web;

namespace XZ.Content.Mailings
{
    public static class Mailings
    {
        public readonly static MailingsSection Settings = (MailingsSection)WebConfigurationManager.GetSection("system.web/mailings");
        private static MailingProvider _provider = null;
        private static MailingProviderCollection _providers = null;
        private static object _lock = new object();

        public static MailingProvider Provider
        {
            get { return _provider; }
        }

        public static MailingProviderCollection Providers
        {
            get { return _providers; }
        }

        // methods that work with newsletters
        public static List<Mailing> GetMailings(DateTime toDate)
        {
            // Make sure a provider is loaded
            LoadProviders();

            // Delegate to the provider
            return _provider.GetMailings(toDate);
        }
        public static Mailing GetMailingByID(Guid mailingID)
        {
            // Make sure a provider is loaded
            LoadProviders();

            // Delegate to the provider
            return _provider.GetMailingByID(mailingID);
        }
        public static bool DeleteMailing(Guid mailingID)
        {
            // Make sure a provider is loaded
            LoadProviders();

            // Delegate to the provider
            return _provider.DeleteMailing(mailingID);
        }
        public static bool UpdateMailing(Mailing mailing)
        {
            // Make sure a provider is loaded
            LoadProviders();

            // Delegate to the provider
            return _provider.UpdateMailing(mailing);
        }
        public static Guid InsertMailing(Mailing mailing)
        {
            // Make sure a provider is loaded
            LoadProviders();

            // Delegate to the provider
            return _provider.InsertMailing(mailing);
        }

        private static void LoadProviders()
        {
            // Avoid claiming lock if providers are already loaded
            if (_provider == null)
            {
                lock (_lock)
                {
                    // Do this again to make sure _provider is still null
                    if (_provider == null)
                    {
                        // Load registered providers and point _provider
                        // to the default provider
                        _providers = new MailingProviderCollection();
                        ProvidersHelper.InstantiateProviders(Settings.Providers, _providers, typeof(MailingProvider));
                        _provider = _providers[Settings.DefaultProvider];

                        if (_provider == null)
                            throw new ProviderException("Unable to load default MailingProvider");
                    }
                }
            }
        }
    }
}